<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class NewsController extends AppController {

    var $name = 'News';
    var $components = array(
        'Session', 'Alias', 'Upload', 'Resize','Crop'
    );
    var $helpers = array(
        'Editor', 'Published'
    );
    var $uses = array(
        'Catnews', 'News'
    );

    function afterFilter() {
        //update cache
        $this->News->cacheLastestNews();
    }

    function index() {

        $this->set('title_for_layout', Configure::read('site_name') . ' - Tin tức');
        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;
        $search = !empty($this->params['form']['search']) ? $this->params['form']['search'] : false;
        $conditions = array();

        //search
        if ($search) {
            $conditions['OR'] = array(
                'News.title LIKE' => "%$search%",
                'News.id LIKE' => "%$search%",
            );
        }
        $this->paginate = array(
            'conditions' => $conditions,
            'limit' => Configure::read('limit_list'),
            'order' => array(
                'News.id' => 'DESC'
            ),
        );
        $data = $this->paginate('News');
        $this->set('total', $this->params['paging']['News']['count']);
        $this->set('data', $data);
        $this->set('limit_page', $this->paginate['limit']);
        $this->set('start_page', $page);
    }

    function remove() {
        $conditions = array(
            "{$this->News->alias}.{$this->News->primaryKey} IN (" . implode(',', $this->params['form']['cid']) . ')'
        );
        //delete thumbnail (132x99)
        $data = $this->News->find('list', array(
                    'conditions' => $conditions,
                    'fields' => array('News.id', 'News.thumbnail')
                        )
        );
        foreach ($data AS $image_file) {
            //delete it
            if (is_file(Configure::read('news_image_absolute_path') . $image_file) && $image_file != 'no_image.gif') {
                $this->Upload->delete(Configure::read('news_image_absolute_path') . $image_file);
            }
        }
        //delete thumbnail small (88x58)
        $data = $this->News->find('list', array(
                    'conditions' => $conditions,
                    'fields' => array('News.id', 'News.thumbnail_small')
                        )
        );
        foreach ($data AS $image_file) {
            //delete it
            if (is_file(Configure::read('news_image_absolute_path') . $image_file) && $image_file != 'no_image.gif') {
                $this->Upload->delete(Configure::read('news_image_absolute_path') . $image_file);
            }
        }
        //delete slideshow images (365x280)
        $data = $this->News->find('list', array(
                    'conditions' => $conditions,
                    'fields' => array('News.id', 'News.thumbnail_slideshow')
                        )
        );
        foreach ($data AS $image_file) {
            //delete it
            if (is_file(Configure::read('news_image_absolute_path') . $image_file) && $image_file != 'no_image.gif') {
                $this->Upload->delete(Configure::read('news_image_absolute_path') . $image_file);
            }
        }
        if ($this->News->deleteAll($conditions, true)) {
            $this->Session->setFlash(sprintf("Xóa thành công %d tin tức", sizeof($this->params['form']['cid'])), 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash('Không thể xóa', 'msg', array(), 'error');
        }
        $this->redirect(array('action' => 'index'));
    }

    function edit() {
        $this->set('title_for_layout', Configure::read('site_name') . ' - Chỉnh sữa thông tin');
        $params = array_merge($this->params['form'], $this->params['named']);
        if (isset($params['cid']) && is_array($params['cid'])) {
            $this->News->id = $params['cid'][0];
        } else {
            $this->News->id = array_key_exists('id', $params) ? $params['id'] : 0;
        }
        if (empty($this->data)) {
            $fields = array(
                '*'
            );
            $this->data = $this->News->read($fields);
            $this->set('cat_list', $this->Catnews->getListAll());
            $check_thumbnail_exist = is_file(Configure::read('news_image_absolute_path') . $this->data['News']['thumbnail']);
            $check_thumbnail_small_exist = is_file(Configure::read('news_image_absolute_path') . $this->data['News']['thumbnail_small']);
            $check_thumbnail_slideshow_exist = is_file(Configure::read('news_image_absolute_path') . $this->data['News']['thumbnail_slideshow']);

            $this->set('check_thumbnail_exist', $check_thumbnail_exist);
            $this->set('check_thumbnail_small_exist', $check_thumbnail_small_exist);
            $this->set('check_thumbnail_slideshow_exist', $check_thumbnail_slideshow_exist);
            $this->set('image_url', Configure::read('news_image_url_path') . $this->data['News']['thumbnail']);
            $this->set('image_small_url', Configure::read('news_image_url_path') . $this->data['News']['thumbnail_small']);
            $this->set('image_slideshow_url', Configure::read('news_image_url_path') . $this->data['News']['thumbnail_slideshow']);

            $this->set('content', $this->data['News']['content']);
        }
    }

    function add() {
        $this->set('cat_list', $this->Catnews->getListAll());
        $this->set('title_for_layout', Configure::read('site_name') . ' - thêm tin tức');
    }

    function crop() {
        $form = $this->params['form'];
        $check  =   false;
        $img_132_99_filename  =   Configure::read('news_image_absolute_path') . $form['img_132_99_filename'];
        if ($this->Crop->loadImage($img_132_99_filename)) {
            $this->Crop->cropToDimensions($form['img_132_99_sx'], $form['img_132_99_sy'],$form['img_132_99_ex'],$form['img_132_99_ey']);
            $this->Crop->saveImage($img_132_99_filename);
            $this->Crop->flushImages();
        }
        $img_88_58_filename  =   Configure::read('news_image_absolute_path') . $form['img_88_58_filename'];
        if ($this->Crop->loadImage($img_88_58_filename)) {
            $this->Crop->cropToDimensions($form['img_88_58_sx'], $form['img_88_58_sy'],$form['img_88_58_ex'],$form['img_88_58_ey']);
            $this->Crop->saveImage($img_88_58_filename);
            $this->Crop->flushImages();
        }
        $img_365_280_filename  =   Configure::read('news_image_absolute_path') . $form['img_365_280_filename'];
        if ($this->Crop->loadImage($img_365_280_filename)) {
            $this->Crop->cropToDimensions($form['img_365_280_sx'], $form['img_365_280_sy'],$form['img_365_280_ex'],$form['img_365_280_ey']);
            $check = $this->Crop->saveImage($img_365_280_filename);
            $this->Crop->flushImages();
        }
        $this->Session->setFlash('đã crop ảnh thành công', 'msg', array(), 'msg');
        
        $this->redirect(array('controller' => 'news', 'action' => 'index'));
    }

    function save() {
        $data = $this->data['News'];

        //for upload thumbnail
        $this->Upload->setUploadpath(Configure::read('news_image_absolute_path'));
        $this->Upload->setRules('size', Configure::read('content_image_maxsize'));
        $this->Upload->setRules('ext', Configure::read('content_image_ext'));
        $row = $this->News->findById($this->data['News']['id']);

        //upload small thumbnail (132x99)
        if ($this->data['News']['thumbnail']['name'] != '') {
            if ($this->Upload->validate($this->data['News']['thumbnail'])) {
                $data['thumbnail'] = $this->Upload->upload($this->data['News']['thumbnail']);
                $this->Resize->run_resize(Configure::read('news_image_absolute_path') . $data['thumbnail'], 132, 99);

                //delete it
                if (is_file(Configure::read('news_image_absolute_path') . $row['News']['thumbnail']) && $row['News']['thumbnail'] != 'no_image.gif') {
                    $this->Upload->delete(Configure::read('news_image_absolute_path') . $row['News']['thumbnail']);
                }
            } else {
                $this->Session->setFlash($this->Upload->getError(), 'error', array(), 'msg');
                $this->redirect(array('controller' => 'news', 'action' => 'index'));
                return;
            }
        } else {
            unset($data['thumbnail']);
            //delete image
            if (!empty($this->data['News']['delete_image'])) {

                if (is_file(Configure::read('news_image_absolute_path') . $row['News']['thumbnail']) && $row['News']['thumbnail'] != 'no_image.gif') {
                    $this->Upload->delete(Configure::read('news_image_absolute_path') . $row['News']['thumbnail']);
                    $data['thumbnail'] = 'no_image.gif';
                }
            }
        }
        //end upload thumbnail
        //for upload thumbnail small 88x58
        //upload small thumbnail small 88x58
        if ($this->data['News']['thumbnail_small']['name'] != '') {
            if ($this->Upload->validate($this->data['News']['thumbnail_small'])) {
                $data['thumbnail_small'] = $this->Upload->upload($this->data['News']['thumbnail_small']);
                $this->Resize->run_resize(Configure::read('news_image_absolute_path') . $data['thumbnail_small'], 88, 58);

                //delete it
                if (is_file(Configure::read('news_image_absolute_path') . $row['News']['thumbnail_small']) && $row['News']['thumbnail_small'] != 'no_image.gif') {
                    $this->Upload->delete(Configure::read('news_image_absolute_path') . $row['News']['thumbnail_small']);
                }
            } else {
                $this->Session->setFlash($this->Upload->getError(), 'error', array(), 'msg');
                $this->redirect(array('controller' => 'news', 'action' => 'index'));
                return;
            }
        } else {
            unset($data['thumbnail_small']);
            //delete image
            if (!empty($this->data['News']['delete_image_small'])) {
                if (is_file(Configure::read('news_image_absolute_path') . $row['News']['thumbnail_small']) && $row['News']['thumbnail_small'] != 'no_image.gif') {
                    $this->Upload->delete(Configure::read('news_image_absolute_path') . $row['News']['thumbnail_small']);
                    $data['thumbnail_small'] = 'no_image.gif';
                }
            }
        }
        //end upload thumbnail small 88x58
        //for upload thumbnail large 365x280
        //upload small thumbnail large 365x280
        if ($this->data['News']['thumbnail_slideshow']['name'] != '') {
            if ($this->Upload->validate($this->data['News']['thumbnail_slideshow'])) {
                $data['thumbnail_slideshow'] = $this->Upload->upload($this->data['News']['thumbnail_slideshow']);
                $this->Resize->run_resize(Configure::read('news_image_absolute_path') . $data['thumbnail_slideshow'], 365, 280);

                //delete it
                if (is_file(Configure::read('news_image_absolute_path') . $row['News']['thumbnail_slideshow']) && $row['News']['thumbnail_slideshow'] != 'no_image.gif') {
                    $this->Upload->delete(Configure::read('news_image_absolute_path') . $row['News']['thumbnail_slideshow']);
                }
            } else {
                $this->Session->setFlash($this->Upload->getError(), 'error', array(), 'msg');
                $this->redirect(array('controller' => 'news', 'action' => 'index'));
                return;
            }
        } else {
            unset($data['thumbnail_slideshow']);
            //delete image
            if (!empty($this->data['News']['delete_image_large'])) {

                if (is_file(Configure::read('news_image_absolute_path') . $row['News']['thumbnail_slideshow']) && $row['News']['thumbnail_slideshow'] != 'no_image.gif') {
                    $this->Upload->delete(Configure::read('news_image_absolute_path') . $row['News']['thumbnail_slideshow']);
                    $data['thumbnail_slideshow'] = 'no_image.gif';
                }
            }
        }
        //end upload thumbnail large 365x280

        $data['alias'] = $this->Alias->alias($data['title']);

        if (isset($data['id'])) {
            $this->News->set('user_modified', $this->Auth->user('id'));
            $this->News->set('user_modified_ip', $_SERVER['REMOTE_ADDR']);
        } else {
            $this->News->set('user_created', $this->Auth->user('id'));
            $this->News->set('user_created_ip', $_SERVER['REMOTE_ADDR']);
        }
        if ($this->News->save($data)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->News->invalidFields(), 'error', array(), 'msg');
        }
        $this->News->writeHomeLastestNews();
        $this->News->writeHomeHotNews();
        $this->News->writeHomeFashionNews();
        $this->News->writeHomeNotice();
        $this->redirect(array('controller' => 'news', 'action' => 'index'));
    }

    function cancel() {
        $this->redirect(array('controller' => 'news', 'action' => 'index'));
    }

    function published() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'published' => 1,
            );
        }

        if ($this->News->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->News->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'news', 'action' => 'index'));
    }

    function unpublished() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'published' => 0,
            );
        }

        if ($this->News->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->News->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'news', 'action' => 'index'));
    }

}

?>
